package com.lantosec.hmrp.modules.category.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.category.entity.AssetsCheckNote;
import com.lantosec.hmrp.modules.category.entity.AssetsImage;
import com.lantosec.hmrp.modules.category.entity.AuditInfo;
import com.lantosec.hmrp.modules.category.service.IAssetsCheckNoteService;
import com.lantosec.hmrp.modules.category.service.IAssetsInfoService;
import com.lantosec.hmrp.modules.category.vo.AssetsCheckNoteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * 盘点记录 控制器
 *
 * @author AutoCode
 * @since 2020-04-02
 */
@Api(value = "盘点记录控制器", tags = "盘点记录控制器")
@RestController
@RequestMapping("/category/checkNote")
@Log4j2
public class AssetsCheckNoteController extends BaseController {
    @Autowired
    private IAssetsCheckNoteService service;

    @Autowired
    private IAssetsInfoService infoService;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<AssetsCheckNoteVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<AssetsCheckNoteVO>> page(
            @ApiParam(value = "分页对象", required = true) Page<AssetsCheckNote> page, AssetsCheckNoteVO condition){
        page = service.page(page,
                new QueryWrapper<AssetsCheckNote>()
//                        .like(StringUtils.isNotEmpty(condition.getUsername()),"username",condition.getUsername())
        );
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<AssetsCheckNoteVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    @ControllerLog(name = "新增盘点计划", type = LogType.add)
    public Result<Integer> create(
            @ApiParam(value = "盘点记录VO对象", required = true) @RequestBody @Validated AssetsCheckNoteVO vo,
            BindingResult bindingResult){
        validationHandler(bindingResult);
        AssetsCheckNote entity = vo.convertToEntity();
        entity.setCheckTime(DateUtils.format(new Date()));
        entity.setReviewState(0);
        entity.setCategoryId(infoService.getById(vo.getAssetsId()).getCategoryId());
        entity.setCheckUserId(getUserId());
        entity.setCheckUser(getName());
        service.save(entity);
        return Result.ok(entity.getNoteId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(
            @ApiParam(value = "盘点记录VO对象", required = true) @RequestBody AssetsCheckNoteVO vo){
        AssetsCheckNote entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("normalUnaudited")
    @ApiOperation("正常未审核")
    public Result<Page<AssetsCheckNoteVO>> normalUnaudited(
            @ApiParam(name = "分页对象",required = true) PageVO<AssetsCheckNote> page, AuditInfo info){
        Page<AssetsCheckNote> param = service.normalUnaudited(page,info);
        return Result.ok(entity2VO(param));
    }

    @GetMapping("changeUnaudited")
    @ApiOperation("状态改变未未审核")
    public Result<Page<AssetsCheckNoteVO>> changeUnaudited(
            @ApiParam(name = "分页对象",required = true) PageVO<AssetsCheckNote> page, AuditInfo info){
        Page<AssetsCheckNote> param = service.changeUnaudited(page,info);
        return Result.ok(entity2VO(param));
    }

    @GetMapping("audited")
    @ApiOperation("已审核")
    public Result<Page<AssetsCheckNoteVO>> audited(
            @ApiParam(name = "分页对象",required = true) PageVO<AssetsCheckNote> page, AuditInfo info){
        Page<AssetsCheckNote> param = service.audited(page,info);
        return Result.ok(entity2VO(param));
    }

    @PostMapping("check")
    @ApiOperation("审核")
    public Result check(
            @ApiParam(value = "审核对象", required = true) @RequestBody Map<String,Object> param){
        service.check(param);
        return Result.ok();
    }

    @GetMapping("queryCheckList")
    @ApiOperation("根据盘点计划Id查询盘点记录")
    public Result<Page<AssetsCheckNote>> queryAssetsCheckNoteList(
            PageVO<AssetsCheckNote> page,
            @ApiParam(value = "区域ID") @RequestParam(name = "areaId",required = false) String areaId,
            @ApiParam(value = "盘点计划") @RequestParam(name = "planId") String planId,
            @ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId,
            @ApiParam(value = "盘点方式") @RequestParam(name = "checkType",required = false) String checkType,
            @ApiParam(value = "盘点人") @RequestParam(name = "checkUserName",required = false) String checkUserName
    ){
        return Result.ok(service.queryAssetsCheckNoteList(page, areaId, deptId, checkUserName,planId,checkType));
    }

    @GetMapping("querySingleCheckList")
    @ApiOperation("查询单个物资盘点记录")
    public Result<List<AssetsCheckNote>> querySingleCheckList(
            @ApiParam(value = "盘点人") @RequestParam(name = "assetsId",required = false) String assetsId
    ){
        return Result.ok(service.querySingleCheckList(assetsId));
    }

    @GetMapping("queryHistoryPic")
    @ApiOperation("查询固定资产历史照片")
    public Result<List<AssetsImage>> queryHistoryPic(
            @ApiParam(value = "盘点人") @RequestParam(name = "assetsId",required = false) String assetsId
    ){
        List<AssetsImage> param = service.queryHistoryPic(assetsId);
        return Result.ok(param);
    }

    @GetMapping("summarize")
    @ApiOperation(value = "盘点概述")
    public Result<Map> summarize(
            @ApiParam(value = "计划ID") @RequestParam(name = "planId") String planId,
            @ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId,
            @ApiParam(value = "区域ID") @RequestParam(name = "areaId",required = false) String areaId
    ){
        Map param = service.summarize(planId,deptId,areaId);
        return Result.ok(param);
    }

    @GetMapping("complete")
    @ApiOperation("查询所有已完成，未完成盘点列表")
    public Result<Map> complete(
            @ApiParam(value = "盘点计划Id") @RequestParam(name = "planId") String planId,
            @ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId,
            @ApiParam(value = "区域ID") @RequestParam(name = "areaId",required = false) String areaId
    ){
        Map param = service.complete(planId,deptId,areaId);
        return Result.ok(param);
    }

    @GetMapping("completeByLableType")
    @ApiOperation("按标签材质查询已完成，未完成盘点列表")
    public Result<Map> completeByLableType(
            @ApiParam(value = "盘点计划Id") @RequestParam(name = "planId") String planId,
            @ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId,
            @ApiParam(value = "区域ID") @RequestParam(name = "areaId",required = false) String areaId,
            @ApiParam(value = "标签材质") @RequestParam(name = "lableType",required = false) String lableType
    ){
        Map param = service.completeByLableType(planId,deptId,areaId,lableType);
        return Result.ok(param);
    }

    @GetMapping("getInfo")
    @ApiOperation(value = "查询固定资产详细信息及盘点信息")
    public Result<AssetsCheckNoteVO> getInfo(
            @ApiParam(value = "固定资产ID", required = true) @RequestParam(name="assetsId") String assetsId,
            @ApiParam(value = "盘点计划ID", required = true) @RequestParam(name="planId") String planId
    ){
        AssetsCheckNote param = service.getInfo(assetsId,planId);
        return Result.ok(entity2VO(param));
    }

    @PostMapping("bulkInventory")
    @ApiOperation(value = "批量盘点")
    public Result<AssetsCheckNoteVO> bulkInventory(
            @ApiParam(value = "固定资产ID" ,required = true) @RequestBody Map<String,Object> map
    ){
        service.bulkInventory(map);
        return Result.ok();
    }
}
